COMP3161/9164 Concepts of Programming Languages
Term 3, 2024

Tute (Week 8)

Table of Contents

1. Data Types

1.1. Converting from Haskell

Use MinHS type constructors \(+\), \(\times\) and \(\mathbf{rec}\) to describe types isomorphic to each of the following Haskell types. Give example terms for each type.

data Direction = East | West | North | South
data Foo = Foo Int Bool Int
data Tree = Node Tree Tree | Leaf

1.2. Recursive Types

Give an example MinHS term for each of the following types (if such a term exists), and derive the typing judgement for that term.

  1. \(\mathbf{rec}\ t.\ (\mathtt{Int} + t)\)
  2. \(\mathbf{rec}\ t.\ (\mathtt{Int} \times \mathtt{Bool})\)
  3. \(\mathbf{rec}\ t.\ (\mathtt{Int} \times t)\)

1.3. Type Isomorphisms

Which of the following types are isomorphic to each other (assuming a strict semantics)?

  1. \(\textbf{rec}\ t.\ t \times t\)
  2. \(\textbf{0}\)
  3. \(\textbf{1}\)
  4. \(\textbf{1} \times \textbf{0}\)
  5. \(\textbf{rec}\ t.\ t \times \textbf{1}\)
  6. \(\textbf{rec}\ t.\ t + \textbf{1}\)

1.4. Typing Terms

For each of the following terms, give a possible type for the term (there may be more than one type).

  1. \((\mathsf{InL}\ \mathsf{True})\)
  2. \((\mathsf{InR}\ \mathsf{True})\)
  3. \((\mathsf{InL}\ (\mathsf{InL}\ \mathsf{True}))\)
  4. \((\mathsf{roll}\ (\mathsf{InR}\ \mathsf{True}))\)
  5. \((\texttt{()}, (\mathsf{InL}\ \texttt{()}))\)

1.5. Curry-Howard

1.5.1. Proof Witnesses

For which of the following types can you write a terminating, total (i.e., not returning error, undefined or causing other runtime errors, but a value of the result type) MinHs function? Try to answer this question without trying to implement the function.

  1. \((a \rightarrow b) \rightarrow (b \rightarrow c) \rightarrow (a \rightarrow c)\)
  2. \(((a \times b) \rightarrow c) \rightarrow (a \rightarrow c)\)
  3. \((a \rightarrow c) \rightarrow ((a \times b) \rightarrow c)\)

What proposition does each of these types correspond to?

1.5.2. Proving a proposition

Write a program which is a proof of the symmetry of disjunction, i.e. \[A \lor B \Rightarrow B \lor A\].

1.5.3. Constructivity

Lambda calculus is said to correspond to constructive logic. What is meant by constructive here?

2. Polymorphism

2.1. Quantifier Positions

Explain the difference between a function of type \(\forall a.\ a \rightarrow a\) and \((\forall a.\ a) \rightarrow (\forall a.\ a)\).

2.2. Parametricity

What can we conclude about the following functions solely from looking at their type signature?

  1. \(f_1 : \forall a.\ [a] \rightarrow a\)
  2. \(f_2 : \forall a.\ \forall b.\ [a] \rightarrow [b]\)
  3. \(f_3 : \forall a.\ \forall b.\ [a] \rightarrow b\)
  4. \(f_4 : [\texttt{Int}] \rightarrow [\texttt{Int}]\)

2024-11-28 Thu 20:06

Announcements RSS